package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/7/10 9:28
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private ManageService manageService;
    /**
     * 保存skuInfo 数据
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        //  调用服务层方法
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //  http://localhost/admin/product/list/1/10?category3Id=61

    /**
     * 查询分页列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result skuInfoList(@PathVariable Long page,
                              @PathVariable Long limit,
                              Long category3Id){
        //  创建page 对象
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        //  调用服务层方法
        IPage<SkuInfo> iPage = this.manageService.getSkuInfoList(skuInfoPage,category3Id);
        //  返回数据
        return Result.ok(iPage);
    }

    /**
     * 商品上架 is_sale = 1;
     * @param skuId
     * @return
     */
    @GetMapping("onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        //  调用服务层方法
        this.manageService.onSale(skuId);
        return Result.ok();
    }
    /**
     * 商品下架 is_sale = 0;
     * @param skuId
     * @return
     */
    @GetMapping("cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        //  调用服务层方法
        this.manageService.cancelSale(skuId);
        return Result.ok();
    }

}
